/*import com.zc.commons.base.Result;
import com.zc.export.bean.ExcelBean;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtil<T>
{
  public static final String EXCEl_FILE_2007 = "2007";
  public static final String EXCEL_FILE_2003 = "2003";
  private Logger logger = Logger.getLogger(getClass());

  public Result createExcel(String moduleXmlName, Map<String, Object> map)
  {
    Result result = new Result();
    try {
      ExcelBean excelBean = createExcelModule(moduleXmlName);
      if (!excelBean.isCreateStatus()) {
        result.setCode("100");
        result.setResult("生成execl  模板失败!");
        return result;
      }
      XLSTransformer transformer = new XLSTransformer();

      SimpleDateFormat sdf = new SimpleDateFormat(Constants.EXPORT_TITLE_FORMAT);

      String title = sdf.format(new Date());

      File file = new File(Constants.APP_PATH + Constants.excel_out_path);
      if (!file.exists()) {
        file.mkdir();
      }

      String outname = Constants.APP_PATH + Constants.excel_out_path + excelBean.getId() + " " + title + Constants.EXPORT_TYPE;

      Object newObj = map.get("model");
      map.put("excelModel", newObj);
      map.put("dateFormat", new SimpleDateFormat(Constants.EXPORT_DATA_FORMAT));

      transformer.transformXLS(Constants.APP_PATH + Constants.module_out_path + excelBean.getTitle() + Constants.EXPORT_TYPE, map, outname);

      result.putData("outPath", outname);

      result.putData("title", excelBean.getId() + " " + title + Constants.EXPORT_TYPE);
    }
    catch (Exception e) {
      this.logger.error(e.getMessage(), e);
      result.setCode("100");
      result.setResult(e.getMessage());
    }
    return result;
  }

  public ExcelBean createExcelModule(String moduleXmlName) {
    ExcelBean excelBean = null;
    if (Constants.EXPORT_VERSION.equals("2007")) {
      this.logger.debug("生成  execl 2007 模板 ：" + moduleXmlName);
      excelBean = create2007ExcelModule(moduleXmlName);
    }
    else if (Constants.EXPORT_VERSION.equals("2003")) {
      this.logger.debug("生成  execl 2003 模板 ：" + moduleXmlName);
      excelBean = create2003ExcelModule(moduleXmlName);
    }
    else {
      this.logger.error("未指定excel  导出版本!");
    }
    return excelBean;
  }

  public ExcelBean create2007ExcelModule(String moduleXmlName)
  {
    ExcelBean excelBean = null;
    try {
      XMLUtil xmlUtil = new XMLUtil();
      excelBean = xmlUtil.loadModule(moduleXmlName);

      File file = new File(Constants.APP_PATH + Constants.module_out_path + excelBean.getId() + Constants.EXPORT_TYPE);

      if (!file.exists()) {
        file.createNewFile();
      }

      XSSFWorkbook workbook = new XSSFWorkbook();

      XSSFSheet sheet = workbook.createSheet(excelBean.getTitle());

      sheet.setDefaultColumnWidth(20);

      XSSFCellStyle style = workbook.createCellStyle();

      style.setFillForegroundColor(new XSSFColor(Color.gray));
      style.setFillPattern((short)1);
      style.setBorderBottom((short)1);
      style.setBorderLeft((short)1);
      style.setBorderRight((short)1);
      style.setBorderTop((short)1);
      style.setAlignment((short)2);

      XSSFFont font = workbook.createFont();
      font.setBoldweight((short)700);
      font.setFontName("宋体");
      font.setColor(new XSSFColor(Color.BLACK));
      font.setFontHeightInPoints((short)11);

      style.setFont(font);

      XSSFCellStyle style2 = workbook.createCellStyle();
      style2.setFillForegroundColor(new XSSFColor(Color.WHITE));
      style2.setFillPattern((short)1);
      style2.setBorderBottom((short)1);
      style2.setBorderLeft((short)1);
      style2.setBorderRight((short)1);
      style2.setBorderTop((short)1);
      style2.setAlignment((short)2);
      style2.setVerticalAlignment((short)1);

      XSSFFont font2 = workbook.createFont();
      font2.setBoldweight((short)400);

      style2.setFont(font2);

      XSSFRow row = sheet.createRow(0);
      XSSFRow contentrow = sheet.createRow(1);

      int titleNum = 0;
      for (com.zc.export.bean.Field field : excelBean.getFieldList())
      {
        XSSFCell cellHeader = row.createCell(titleNum);
        cellHeader.setCellStyle(style);
        cellHeader.setCellValue(new XSSFRichTextString(field.getName()));

        XSSFCell cell = contentrow.createCell(titleNum);
        cell.setCellStyle(style2);
        if ((field.getIsFormat() != null) && (!field.getIsFormat().equals(""))) {
          if (field.getIsFormat().equals("true"))
          {
            cell.setCellValue(new XSSFRichTextString("${dateFormat.format(excelModel." + field.getId() + ")}"));
          }
          else
            cell.setCellValue(new XSSFRichTextString("${excelModel." + field.getId() + "}"));
        }
        else {
          cell.setCellValue(new XSSFRichTextString("${excelModel." + field.getId() + "}"));
        }

        titleNum++;
      }

      OutputStream out = new FileOutputStream(file);
      workbook.write(out);
      out.close();
      excelBean.setCreateStatus(true);
    } catch (Exception e) {
      this.logger.error(e.getMessage(), e);
      excelBean.setCreateStatus(false);
    }
    return excelBean;
  }

  public ExcelBean create2003ExcelModule(String moduleXmlName)
  {
    ExcelBean excelBean = null;
    try {
      XMLUtil xmlUtil = new XMLUtil();
      excelBean = xmlUtil.loadModule(moduleXmlName);
      File file = new File(Constants.APP_PATH + Constants.module_out_path + excelBean.getTitle() + Constants.EXPORT_TYPE);

      if (!file.exists()) {
        file.createNewFile();
      }

      HSSFWorkbook workbook = new HSSFWorkbook();

      HSSFSheet sheet = workbook.createSheet(excelBean.getTitle());

      sheet.setDefaultColumnWidth(20);

      HSSFCellStyle style = workbook.createCellStyle();

      style.setFillForegroundColor((short)22);
      style.setFillPattern((short)1);
      style.setBorderBottom((short)1);
      style.setBorderLeft((short)1);
      style.setBorderRight((short)1);
      style.setBorderTop((short)1);
      style.setAlignment((short)2);

      HSSFFont font = workbook.createFont();
      font.setBoldweight((short)700);
      font.setFontName("宋体");
      font.setColor((short)9);
      font.setFontHeightInPoints((short)11);

      style.setFont(font);

      HSSFCellStyle style2 = workbook.createCellStyle();
      style2.setFillForegroundColor((short)9);
      style2.setFillPattern((short)1);
      style2.setBorderBottom((short)1);
      style2.setBorderLeft((short)1);
      style2.setBorderRight((short)1);
      style2.setBorderTop((short)1);
      style2.setAlignment((short)2);
      style2.setVerticalAlignment((short)1);

      HSSFFont font2 = workbook.createFont();
      font2.setBoldweight((short)400);

      style2.setFont(font2);

      HSSFRow row = sheet.createRow(0);
      HSSFRow contentrow = sheet.createRow(1);

      int titleNum = 0;
      for (com.zc.export.bean.Field field : excelBean.getFieldList())
      {
        HSSFCell cellHeader = row.createCell(titleNum);
        cellHeader.setCellStyle(style);
        cellHeader.setCellValue(new HSSFRichTextString(field.getName()));

        HSSFCell cell = contentrow.createCell(titleNum);
        cell.setCellStyle(style2);
        if ((field.getIsFormat() != null) && (!field.getIsFormat().equals(""))) {
          if (field.getIsFormat().equals("true"))
          {
            cell.setCellValue(new HSSFRichTextString("${dateFormat.format(excelModel." + field.getId() + ")}"));
          }
          else
            cell.setCellValue(new HSSFRichTextString("${excelModel." + field.getId() + "}"));
        }
        else {
          cell.setCellValue(new HSSFRichTextString("${excelModel." + field.getId() + "}"));
        }

        titleNum++;
      }

      OutputStream out = new FileOutputStream(file);
      workbook.write(out);
      out.close();
      excelBean.setCreateStatus(true);
    } catch (Exception e) {
      this.logger.error(e.getMessage(), e);
      excelBean.setCreateStatus(false);
    }
    return excelBean;
  }

  public void exportExcel2003(String title, String[] headers, Collection<T> dataset, OutputStream out, String pattern)
  {
    HSSFWorkbook workbook = new HSSFWorkbook();

    HSSFSheet sheet = workbook.createSheet(title);

    sheet.setDefaultColumnWidth(20);

    HSSFCellStyle style = workbook.createCellStyle();

    style.setFillForegroundColor((short)12);
    style.setFillPattern((short)1);
    style.setBorderBottom((short)1);
    style.setBorderLeft((short)1);
    style.setBorderRight((short)1);
    style.setBorderTop((short)1);
    style.setAlignment((short)2);

    HSSFFont font = workbook.createFont();
    font.setBoldweight((short)700);
    font.setFontName("宋体");
    font.setColor((short)9);
    font.setFontHeightInPoints((short)11);

    style.setFont(font);

    HSSFCellStyle style2 = workbook.createCellStyle();
    style2.setFillForegroundColor((short)9);
    style2.setFillPattern((short)1);
    style2.setBorderBottom((short)1);
    style2.setBorderLeft((short)1);
    style2.setBorderRight((short)1);
    style2.setBorderTop((short)1);
    style2.setAlignment((short)2);
    style2.setVerticalAlignment((short)1);

    HSSFFont font2 = workbook.createFont();
    font2.setBoldweight((short)400);

    style2.setFont(font2);

    HSSFRow row = sheet.createRow(0);

    for (int i = 0; i < headers.length; i++) {
      HSSFCell cellHeader = row.createCell(i);
      cellHeader.setCellStyle(style);
      cellHeader.setCellValue(new HSSFRichTextString(headers[i]));
    }

    Iterator it = dataset.iterator();
    int index = 0;

    Pattern p = Pattern.compile("^//d+(//.//d+)?$");

    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    while (it.hasNext()) {
      index++;
      row = sheet.createRow(index);
      Object t = it.next();

      java.lang.reflect.Field[] fields = t.getClass().getDeclaredFields();
      for (int i = 0; i < fields.length; i++) {
        HSSFCell cell = row.createCell(i);
        cell.setCellStyle(style2);
        java.lang.reflect.Field field = fields[i];
        String fieldName = field.getName();
        String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        try {
          Class tCls = t.getClass();
          Method getMethod = tCls.getMethod(getMethodName, new Class[0]);
          Object value = getMethod.invoke(t, new Object[0]);

          String textValue = null;
          if ((value instanceof Integer)) {
            cell.setCellValue(((Integer)value).intValue());
          } else if ((value instanceof Float)) {
            textValue = String.valueOf((Float)value);
            cell.setCellValue(textValue);
          } else if ((value instanceof Double)) {
            textValue = String.valueOf((Double)value);
            cell.setCellValue(textValue);
          } else if ((value instanceof Long)) {
            cell.setCellValue(((Long)value).longValue());
          }
          if ((value instanceof Boolean)) {
            textValue = "是";
            if (!((Boolean)value).booleanValue())
              textValue = "否";
          }
          else if ((value instanceof Date)) {
            textValue = sdf.format((Date)value);
          }
          else if (value != null) {
            textValue = value.toString();
          }

          if (textValue != null) {
            Matcher matcher = p.matcher(textValue);
            if (matcher.matches())
            {
              cell.setCellValue(Double.parseDouble(textValue));
            } else {
              HSSFRichTextString richString = new HSSFRichTextString(textValue);
              cell.setCellValue(richString);
            }
          }
        } catch (SecurityException e) { e = 
            e;

          e.printStackTrace(); } catch (NoSuchMethodException e) {
          e = 
            e;

          e.printStackTrace(); } catch (IllegalArgumentException e) {
          e = 
            e;

          e.printStackTrace(); } catch (IllegalAccessException e) {
          e = 
            e;

          e.printStackTrace(); } catch (InvocationTargetException e) {
          e = 
            e;

          e.printStackTrace();
        }
        finally {
        }
      }
    }
    try {
      workbook.write(out);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}*/